<?php

class Eleven_Resource_Student_Topic extends CMS_Model_Resource_Db_Table_Abstract
{
	protected $_name = 'studenttopic';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Student_Topic_Item';
	
	public function getCurrentTopic($topicId)
	{
	    $select = $this->select();
	    if (null == $topicId)
	    {
		    $order = 'id DESC';
    		$select->order($order);
    		$select->limit(1);
	    }else{
	        return $this->find($topicId)->current();
	    }
		
		return $this->fetchAll ( $select )->current();
	}
	
	public function deleteTopicById($topicId, $contents)
	{
		foreach ($contents as $content)
		{
			$content->delete();
		}
		
		$this->find($topicId)->current()->delete();
	}
	
    public function getTopics($paged = false, $order = null)
	{
	    $select = $this->select ();
		$select->from ( 'studenttopic' );
		
		$select->order ( $order );
		
		if (null !== $paged) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'studenttopic', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 12 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
		return $this->fetchAll ( $select );
	}
}

?>